﻿using System.Windows;
using System.Windows.Controls;
using Microsoft.Win32;
using System;
using System.Reflection;
using System.ComponentModel;
using System.Diagnostics;

using System.ComponentModel.Composition;

namespace wiki2doc
{
    public class IniConfiguration : IConfiguration, INotifyPropertyChanged
    {
        private Ini.IniFile _ini;

        public string WikiType { get; set; }
        public string URL { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
        public string Namespace  { get; set; }

        public string Title { get; set; }
        public string Author { get; set; }
        public string Language { get; set; }

        public string Filename { get; set; }
        public string Folder { get; set; }

        public string TemplatePath { get; set; }

        public IniConfiguration()
        {
            Debug.WriteLine("IniConfiguration constructor");
        }

        public string Select()
        {
            // Create OpenFileDialog
            var dlg = new OpenFileDialog();

            // Set filter for file extension and default file extension
            dlg.DefaultExt = ".ini";
            dlg.Filter = "Wiki configuration (.ini)|*.ini";

            // Display OpenFileDialog by calling ShowDialog method
            Nullable<bool> result = dlg.ShowDialog();

            if (result == true)
            {
                Select(dlg.FileName);
            }
            return dlg.FileName;
        }

        public void Select(string confname)
        {
            _ini = new Ini.IniFile(confname);

            // General
            TemplatePath = _ini.IniReadValue("general", "template");
            var _wiki = _ini.IniReadValue("general", "wikitype", "DokuWiki");
            WikiType = _wiki;

            // wiki settings
            URL = _ini.IniReadValue(_wiki, "url");
            Username = _ini.IniReadValue(_wiki, "username");
            Password = _ini.IniReadValue(_wiki, "password");
            Namespace = _ini.IniReadValue(_wiki, "namespace");

            // document settings
            Title = _ini.IniReadValue("document", "title", "");
            Author = _ini.IniReadValue("document", "author", "");
            Language = _ini.IniReadValue("document", "lang", "nl");
        
            Filename = _ini.IniReadValue("document", "filename", "");
            Folder = _ini.IniReadValue("document", "folder", "");

           

        
        }
        public void Store()
        {
            if ( _ini == null ) return;

            var _wiki = WikiType;
            _ini.IniWriteValue("general", "wikitype", _wiki);

            // dokuwiki settings
            _ini.IniWriteValue(_wiki, "url", URL);
            _ini.IniWriteValue(_wiki, "namespace", Namespace);
            _ini.IniWriteValue(_wiki, "username", Username);
            _ini.IniWriteValue(_wiki, "password", Password);

            // document settings
            _ini.IniWriteValue("document", "title", Title);
            _ini.IniWriteValue("document", "author", Author);
            _ini.IniWriteValue("document", "lang", Language);

            // output
            _ini.IniWriteValue("document", "filename", Filename);
            _ini.IniWriteValue("document", "folder", Folder);

            // template path
            _ini.IniWriteValue("general", "template", TemplatePath);
        }



        #region INotifyPropertyChanged implementation

        public event PropertyChangedEventHandler PropertyChanged;


        protected void Notify(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion INotifyPropertyChanged implementation

    }
}
